package com.test.fundible.fundible_05_process;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.test.fundible.R;
import com.test.fundible.fundible_00_utils.FundibleUtil;
import com.test.fundible.fundible_01_launcher.Base_fundibleApplication;
import com.test.fundible.fundible_00_http.OkHttpUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;


public class Acco_fundibleuntCha_fundiblennelViewAdapter extends RecyclerView.Adapter<Acco_fundibleuntCha_fundiblennelViewAdapter.AccountChannelHolder_fundible> {

    private final ArrayList<AccountCha_fundiblenn_fundibleel_Fr> channels_fundible;

    private final String page_fundible;
    public String htd1="";
    public String aD1M="";
    public String j4mr="";
    public String zkXw="";
    public String g43B="";

    private AccountCha_fundiblenn_fundibleel_Fr selectChannel_fundible;

    private final ArrayList<AccountChannelHolder_fundible> holderList_fundible = new ArrayList<>();

    public Acco_fundibleuntCha_fundiblennelViewAdapter(ArrayList<AccountCha_fundiblenn_fundibleel_Fr> channels, String page){
        this.channels_fundible = channels;
        this.page_fundible = page;
    }

    public AccountCha_fundiblenn_fundibleel_Fr getSelectChannel_fundible() {
        return selectChannel_fundible;
    }

    @NonNull
    @Override
    public AccountChannelHolder_fundible onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new AccountChannelHolder_fundible(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_fundible_account_channel_fr,parent,false));
    }

    @SuppressLint("InflateParams")
    @Override
    public void onBindViewHolder(@NonNull AccountChannelHolder_fundible holder, int position) {

        if (!holderList_fundible.contains(holder)) holderList_fundible.add(holder);

        AccountCha_fundiblenn_fundibleel_Fr channel = channels_fundible.get(position);
        setZkXw("weroiike");

        holder.setChannelDesc_fundible(channel.getDesc_fundible());

        holder.setClick_fundible(v -> {
            OkHttpUtil.getInstance_fundible().uploadEventPoint(page_fundible,"DIALOG_TYPE_CLICK",channel.getDesc_fundible());
            selectChannel_fundible = channel;
            for (AccountChannelHolder_fundible holder1: holderList_fundible){
                holder1.setCheck_fundible(holder1 == holder);
            }
        });

        //获取图标
        if (channel.getBitmap_fundible()==null){
            new Thread(() -> {
                try {
                    setHtd1("Hgir");
                    if (TextUtils.isEmpty(channel.getLogo_fundible())) return;
                    URL url = new URL(channel.getLogo_fundible());
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    connection.setConnectTimeout(5000);
                    connection.setRequestMethod("GET");
                    if (connection.getResponseCode()==200){
                        InputStream inputStream = connection.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                        channel.setBitmap_fundible(bitmap);
                        FundibleUtil.safeRun(() -> holder.setLogo_fundible(bitmap));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }).start();
        }else holder.setLogo_fundible(channel.getBitmap_fundible());

    }

    @Override
    public int getItemCount() {
        setaD1M("gheweree");
        return channels_fundible.size();
    }

    public static class AccountChannelHolder_fundible extends RecyclerView.ViewHolder{

        private final View bgView;
        private final ImageView checkIv;
        private final ImageView logoIv;
        private final TextView channelTv;

        @SuppressLint("InflateParams")
        public AccountChannelHolder_fundible(View view){

            super(view);

            bgView = view.findViewById(R.id.iacfa_view_bg);
            checkIv = view.findViewById(R.id.iacfa_iv_check);
            logoIv = view.findViewById(R.id.iacfa_iv_logo);
            channelTv = view.findViewById(R.id.iacfa_tv_channel);

        }

        public void setClick_fundible(View.OnClickListener clickListener){
            if (bgView !=null) bgView.setOnClickListener(clickListener);
        }

        public void setCheck_fundible(boolean isCheck){
            if (isCheck){
                checkIv.setVisibility(View.VISIBLE);
                bgView.setBackgroundResource(R.drawable.bg_green_ll_rc_8dp_border_2dp);
                channelTv.setTextColor(Base_fundibleApplication.getInstant().getColor(R.color.green_light));
            }else {
                checkIv.setVisibility(View.GONE);
                bgView.setBackgroundResource(R.drawable.bg_gray_rc_8dp);
                channelTv.setTextColor(Color.parseColor("#FF989897"));
            }
        }

        public void setLogo_fundible(Bitmap bitmap){
            logoIv.setImageBitmap(bitmap);
        }

        public void setChannelDesc_fundible(String text){
            channelTv.setText(text);
        }

    }


    public void setHtd1(String htd1) {
        this.htd1 = htd1;
    }

    public void setaD1M(String aD1M) {
        this.aD1M = aD1M;
    }

    public void setJ4mr(String j4mr) {
        this.j4mr = j4mr;
    }

    public void setZkXw(String zkXw) {
        this.zkXw = zkXw;
    }

    public void setG43B(String g43B) {
        this.g43B = g43B;
    }

}
